const express = require('express');

const db = require('../db')

const router =express.Router();


// -----------------------------------------------------获取系统消息--------------------------------------------------------
router.get('/get_system_info',(req,res)=>{
    const {limit,page}=req.query
    let start;
    let sql='select * from  system_info order by create_time desc'
    if (limit && page) {
        start=` limit ${(page-1)*limit},${limit}`
        sql +=start;
    }
    console.log(req);
    db(sql,(err,data)=>{
        if (err) {
            throw err
        }
        db('select count(*) as count from system_info',(err,total)=>{
            if (err) {
                throw err
            }
            res.send({
                code:1,
                msg:'获取成功',
                data:{
                    count:total[0].count,
                    list:data
                }

            })
        })
    })
})


// -----------------------------------------------------添加系统消息--------------------------------------------------------
router.post('/set_system_info',(req,res)=>{
    const {title,content}=req.body;
    const now = Date.parse(new Date())/1000
    db(`insert into system_info(title,content,create_time) values ('${title}','${content}',${now})`,(err,data)=>{
        if (err) {
            res.send({
                code:-1,
                msg:err.sqlMessage,
            })
            throw err ;
        }
        res.send({
            code:1,
            msg:'添加成功！'
        })
    })
})


// -----------------------------------------------------删除系统消息--------------------------------------------------------
router.post('/del_system_info',(req,res)=>{
    const {id}=req.body;
    db(`delete from system_info where system_info_id ='${id}'`,(err,data)=>{
        if (err) {
            res.send({
                code:-1,
                msg:err.sqlMessage,
            })
            throw err ;
        }
        res.send({
            code:1,
            msh:'删除成功！'
        })
    })
})


// -----------------------------------------------------批量删除消息--------------------------------------------------------
router.post('/batch_del_system_info',(req,res)=>{
    const {ids}=req.body;
    db(`delete from system_info where system_info_id  in (${ids})`,(err,data)=>{
        if (err) {
            res.send({
                code:-1,
                msg:err.sqlMessage,
            })
            throw err ;
        }
        res.send({
            code:1,
            msh:'删除成功！'
        })
    })
})





module.exports=router